package dao.impl;

import common.DBConnector;
import common.EntityChange;
import dao.dai.CommodityDao;
import entity.Commodity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CommodityImpl implements CommodityDao {
    private final Connection conn = DBConnector.getSQLServerConnection();

    @Override
    public boolean insert(Commodity commodity) throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement("INSERT INTO Commodity VALUES (?,?,?,?)");
        preparedStatement.setString(1,commodity.getcId());
        preparedStatement.setString(2,commodity.getcName());
        preparedStatement.setString(3, String.valueOf(commodity.getcKind()));
        preparedStatement.setFloat(4,commodity.getcPrice());
        return preparedStatement.execute();
    }

    @Override
    public boolean delete(String cId) throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement("DELETE From Commodity WHERE cId=?");
        preparedStatement.setString(1,cId);
        return preparedStatement.execute();
    }

    @Override
    public boolean update(String oldcId,Commodity commodity) throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement("UPDATE Commodity SET cId=?,cName=?,cKind=?,cPrice=? WHERE cId=?");
        preparedStatement.setString(1,commodity.getcId());
        preparedStatement.setString(2,commodity.getcName());
        preparedStatement.setString(3, String.valueOf(commodity.getcKind()));
        preparedStatement.setFloat(4,commodity.getcPrice());
        preparedStatement.setString(5,oldcId);
        return preparedStatement.execute();
    }

    @Override
    public Commodity search_id(String cId) throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement("SELECT * FROM Commodity WHERE cId=?");
        preparedStatement.setString(1,cId);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next())
            return EntityChange.CommodityChange(resultSet)!=null?EntityChange.CommodityChange(resultSet):null;
        return null;
    }

    @Override
    public List<Commodity> search_name(String cName) throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement("SELECT * FROM Commodity WHERE cName=?");
        preparedStatement.setString(1,cName);
        ResultSet resultSet = preparedStatement.executeQuery();
        List<Commodity> list = new ArrayList<>();
        while (resultSet.next()){
            list.add(EntityChange.CommodityChange(resultSet));
        }
        return list;
    }

    @Override
    public List<Commodity> findAll() throws SQLException {
        PreparedStatement preparedStatement  = conn.prepareStatement("SELECT * FROM Commodity");
        ResultSet resultSet = preparedStatement.executeQuery();
        List<Commodity> list = new ArrayList<>();
        while (resultSet.next()){
            list.add(EntityChange.CommodityChange(resultSet));
        }
        return list;
    }
}
